package fr.xebia.view;

import java.util.Collections;
import java.util.List;

import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ModelDriven;

import fr.xebia.data.ContractType;
import fr.xebia.service.ContractTypeService;

/**
 * 
 */
public class ContratTypeAction extends GenericAction implements ModelDriven<ContractType> {

	private static final long serialVersionUID = 1L;

	private ContractTypeService contratTypeService;

	private ContractType contratType;

	public ContractType getContratType() {
		return contratType;
	}

	/**
	 * @see com.opensymphony.xwork2.ModelDriven#getModel()
	 */
	public ContractType getModel() {
		contratType = new ContractType();
		return contratType;
	}

	/**
	 * @see com.opensymphony.xwork2.Preparable#prepare()
	 */
	@Override
	public void prepare() throws Exception {
		super.prepare();
		logger.debug("Preparing " + getClass().getName());
		contratTypeService = serviceLocator.getContractTypeService();
	}

	public String prepareadd() throws Exception {
		return Action.SUCCESS;
	}

	public String list() throws Exception {
		try {
			List<ContractType> contratTypes = contratTypeService.list();
			request.setAttribute("contratTypes", contratTypes);
			logger.debug("Found " + contratTypes.size() + " contratTypes");
			return Action.SUCCESS;
		} catch (Exception e) {
			request.setAttribute("contratTypes", Collections.EMPTY_LIST);
			return handleError("Can't get contratTypes", e);
		}
	}

	public String search() throws Exception {
		try {
			List<ContractType> contratTypes = contratTypeService
					.search(contratType);
			request.setAttribute("contratTypes", contratTypes);
			logger.debug("Found " + contratTypes.size() + " contratTypes");
			return Action.SUCCESS;
		} catch (Exception e) {
			request.setAttribute("contratTypes", Collections.EMPTY_LIST);
			return handleError("Can't get contratTypes", e);
		}
	}

	public String add() throws Exception {
		try {
			logger.info("Adding new contratType");
			Long id = (Long) contratTypeService.save(contratType);
			contratType.setId(id);
			addActionMessage("Added new contratType with id: "
					+ contratType.getId());
			return Action.SUCCESS;
		} catch (Exception e) {
			return handleError("Can't add contratType", e);
		}
	}

	public String update() throws Exception {
		try {
			logger.info("Updating contratType with id: " + contratType.getId());
			contratTypeService.update(contratType);
			addActionMessage("Updated contratType with id: "
					+ contratType.getId());
			return Action.SUCCESS;
		} catch (Exception e) {
			return handleError("Can't save contratType", e);
		}
	}

	public String edit() throws Exception {
		try {
			logger.info("Getting contratType with id: " + contratType.getId());
			contratType = contratTypeService.get(contratType.getId());
			return Action.SUCCESS;
		} catch (Exception e) {
			return handleError("Can't get contratType", e);
		}
	}

	public String delete() throws Exception {
		try {
			logger.info("Deleting contratType with id: " + contratType.getId());
			contratTypeService.deleteById(contratType.getId());
			addActionMessage("Deleted contratType with id: "
					+ contratType.getId());
			return Action.SUCCESS;
		} catch (Exception e) {
			return handleError("Can't delete contratType", e);
		}
	}
}
